// Created by carolsail

import { ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import DataManager from "../manager/DataManager";
import ToastManager from "../manager/ToastManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class AuthNoticeLayer extends BaseLayer {

    panel: cc.Node = null
    btnSubmitOn: cc.Node = null
    btnSubmitOff: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnSubmitOn = cc.find('btn_submit/on', this.panel)
        this.btnSubmitOff = cc.find('btn_submit/off', this.panel)
    }

    onDestroy() { }

    onEnable() {
        this.zoomIn(this.panel)
        this.btnSubmitOn.active = true
        this.btnSubmitOff.active = false
        SdkManager.instance.getLoginInfo(info => {
            // 授权登录
            if (info.status) {
                this.btnSubmitOn.active = false
                this.btnSubmitOff.active = true
                SdkManager.instance.getLoginCode(async obj => {
                    const data = await DataManager.instance.wsClient.callApi('Login', { code: obj.data })
                    if (!data.isSucc) {
                        ToastManager.instance.show('授权失败：服务器异常', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
                        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AUTH_NOTICE, false)
                        return
                    }
                    // 授权登录成功，缓存数据
                    DataManager.instance.user = info.data
                    DataManager.instance.user.openid = data.res.openid
                    DataManager.instance.save()
                    // 进入大厅
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AUTH_NOTICE, false)
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.HALL)
                })
            } else {
                ToastManager.instance.show('授权失败：拒绝授权', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AUTH_NOTICE, false)
            }
        })
    }

    onDisable() { }


}
